﻿using System;
using System.Data;
using System.Web.UI;
using MyWeb.Business;
using MyWeb.Data;

namespace MyWeb.Admin.Usercontrols
{
    public partial class ucAdvertiseAddUpdate : UserControl
    {
        private readonly AdvertiseService _advertiseService = new AdvertiseService();
        private readonly DistributionService _distributionService = new DistributionService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDdl();
                int insert = Int32.Parse(Request.QueryString["in"]);
                if (insert == 1)
                {
                    RefreshControl();
                }
                else
                {
                    int id = Int32.Parse(Request.QueryString["id"]);
                    DataTable dt = _advertiseService.Advertise_GetById(id);
                    txtName.Text = dt.Rows[0]["name"].ToString();
                    txtImage.Text = dt.Rows[0]["url"].ToString();
                    txtWidth.Text = dt.Rows[0]["width"].ToString();
                    txtHeight.Text = dt.Rows[0]["height"].ToString();
                    txtLink.Text = dt.Rows[0]["link"].ToString();
                    ddlTarget.SelectedValue = dt.Rows[0]["target"].ToString();
                    txtOrder.Text = dt.Rows[0]["order"].ToString();
                    ddlDistribution.SelectedValue = dt.Rows[0]["distribution_id"].ToString();
                    ddlPosition.SelectedValue = dt.Rows[0]["Position"].ToString();
                    ckCongbo.Checked = Int32.Parse(dt.Rows[0]["status"].ToString()) == 0;
                }
            }
        }

        private void RefreshControl()
        {
            txtName.Text = string.Empty;
            txtOrder.Text = string.Empty;
            txtWidth.Text = string.Empty;
            txtHeight.Text = string.Empty;
            txtImage.Text = string.Empty;
            txtLink.Text = string.Empty;
        }

        private void LoadDdl()
        {
            ddlDistribution.DataSource = _distributionService.Distribution_GetByAll();
            ddlDistribution.DataTextField = "distribution";
            ddlDistribution.DataValueField = "id";
            ddlDistribution.DataBind();
        }

        protected void btnCancelAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/View.aspx?action=ucAdvertiseView");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var advertise = new AdvertiseInfo
                                {
                                    Name = txtName.Text,
                                    Url = txtImage.Text,
                                    Width = Int32.Parse(txtWidth.Text),
                                    Height = Int32.Parse(txtHeight.Text),
                                    Link = txtLink.Text,
                                    Target = Int32.Parse(ddlTarget.SelectedValue),
                                    Distribution_Id = Int32.Parse(ddlDistribution.SelectedValue),
                                    Position = Int32.Parse(ddlPosition.SelectedValue),
                                    Order = Int32.Parse(txtOrder.Text),
                                    Status = status
                                };

            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _advertiseService.Advertise_Insert(advertise);
            }
            else
            {
                advertise.Id = Int32.Parse(Request.QueryString["id"]);
                _advertiseService.Advertise_Update(advertise);
            }
            Response.Redirect("~/Admin/View.aspx?action=ucAdvertiseView");
        }

        protected void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            int status = ckCongbo.Checked ? 0 : 1;
            var advertise = new AdvertiseInfo
                                {
                                    Name = txtName.Text,
                                    Url = txtImage.Text,
                                    Width = Int32.Parse(txtWidth.Text),
                                    Height = Int32.Parse(txtHeight.Text),
                                    Link = txtLink.Text,
                                    Target = Int32.Parse(ddlTarget.SelectedValue),
                                    Distribution_Id = Int32.Parse(ddlDistribution.SelectedValue),
                                    Position =  Int32.Parse(ddlPosition.SelectedValue),
                                    Order = Int32.Parse(txtOrder.Text),
                                    Status = status
                                };

            if (Int32.Parse(Request.QueryString["in"]) == 1)
            {
                _advertiseService.Advertise_Insert(advertise);
            }
            else
            {
                advertise.Id = Int32.Parse(Request.QueryString["id"]);
                _advertiseService.Advertise_Update(advertise);
            }
            RefreshControl();
        }
    }
}